package org.judo.admin;

import org.judo.admin.identity.Role;
import org.judo.components.uicontrol.ChoiceControl;
import org.judo.controller.PageController;
import org.judo.database.Database;
import org.judo.database.JudoResultSet;
import org.judo.database.exceptions.DatabaseConnectionException;
import org.judo.database.exceptions.DatabaseException;
import org.judo.database.exceptions.ORMException;
import org.judo.filter.ResultsFilter;
import org.judo.propertyobject.Prop;
import org.judo.propertyobject.PropertyObject;
import org.judo.propertyobject.ValidationError;

import java.util.*;
import org.judo.controller.DefaultAction;

public class RoleController extends PageController 
{
	private Database sysDB ;
	private int id ;
	private int roleid ;
	private String rolename;
	private List results = new ArrayList();
	
	@Prop private int userid;
	
	@Prop private List roleUsers = new ArrayList();
	@Prop private int addedUser;
	@Prop private ChoiceControl active = new ChoiceControl();
	@Prop private ChoiceControl users = new ChoiceControl();
	
	public void componentCreated() throws Exception
	{
		active.addOption("NA","Select Option");
		active.addOption(1,"Active");
		active.addOption(2,"Inactive");
	}

	public void removeUserFromRole()
		throws Exception
	{
		sysDB.exec(parse("delete from User_Role where roleid= ${roleid} and userid = ${userid}"));
		showPage(parse("/admin/Role_controller/detail_role.do?id=${roleid}"));
	}
	
	public void addUserToRole() 
		throws Exception
	{
		JudoResultSet rs = sysDB.query(parse("select * from User_Role where roleId = ${roleid} and userId = ${addedUser}"));
		
		if (rs.size() != 0)
		{
			id = roleid;
			throw new ValidationError("This user is already in the group");
		}
		
		Role role = (Role)sysDB.find(Role.class,roleid);
		role.assignUserToRole(addedUser);
		sysDB.update(role);
		
		showPage(parse("/admin/role_controller/detail_role.do?id=${roleid}"));
	}
	
	public void editRole() throws Exception
	{
		
		PropertyObject obj = sysDB.find(parse("select * from Role where ROLEID = ${id}"));
		copyProps(obj);
	}

	public void setId(int id) 
	{
		this.id = id;
	}

	public int getId() 
	{
		return this.id;
	}

	public void setRoleid(int roleid) 
	{
		this.roleid = roleid;
	}

	public int getRoleid() 
	{
		return this.roleid;
	}

	public void updateRole() throws Exception
	{
		showPage("/admin/Role_controller/display_role.do");
		Role obj = (Role)sysDB.find(Role.class,id);
		obj.copyProps(this);
		sysDB.update("ROLE",obj);
	}

	public void setRolename(String rolename) 
	{
		this.rolename = rolename;
	}

	public String getRolename() 
	{
		return this.rolename;
	}

	public void createRole() throws Exception
	{
	}

	public void insertRole() throws Exception
	{
		showPage("/admin/Role_controller/display_role.do");
		Role obj = new Role();
		obj.copyProps(this);
		sysDB.insert("ROLE",obj);
	}

	@DefaultAction
	public void displayRole() throws Exception
	{
		results = (ArrayList)sysDB.query("select * from ROLE");
		results = ResultsFilter.filterResults(new RoleResultsFilter(),results);
	}

	public void setResults(List results) 
	{
		this.results = results;
	}

	public List getResults() 
	{
		return this.results;
	}

	public void detailRole() throws Exception
	{
		info("ID: "+id);
		Role obj = (Role)sysDB.find(Role.class,parse("select * from ROLE where ROLEID = ${id}"));
		copyProps(obj);
		
		users.define(sysDB.query("select username as display,userid as code from App_User order by username"));
		
		roleUsers = sysDB.query(
				parse("select * from User_role,App_User,Role where User_role.userId = App_User.userId and User_role.roleId = Role.roleId and Role.roleId = ${id}"));
	}

	public void deleteRole() throws Exception
	{
		showPage("/admin/Role_controller/display_role.do");
		sysDB.delete(this,"delete from ROLE where ROLEID = ${id}");
	}

}
